# cc0 license
# credit :mn,richitaroy,ankitaroy


demo = """while 1:
x print

for x in range(5, 7):
p print
end
break"""

from cbCompilation import *


def main():
    global demo
    try:
        file = open("first.txt", "r")
        lines = file.readlines()
        file.close()
    except:
        lines = demo.replace("\r", "").split("\n")

    outfile = open("first.txt.py", "w")  # writing a file

    arr = list()
    outfile.write("from cbCompilation import* \r\n")  # importing cb functions
    indent = 0

    for line in lines:
        line = line.strip()
        arr.append(line)

    for i in range(len(arr)):

        if cbleft(arr[i], 1) == "#":  # for commented out lines
            pass


        elif cbleft(arr[i],4) == "list" :
            si = arr[i].index("t") +1
            li = arr[i].index("=")
            va_name = (arr[i][si:li]).strip()
            command1 = va_name + "= list()"
            outfile.write(stringindent(command1, indent))
            command2 = (arr[i][si:]).strip()
            outfile.write(stringindent(command2, indent))


        elif cbright(arr[i], 5).lower() == "print" and cbleft(arr[i],
                                                            2).lower() != "cb" and cbleft(arr[i],4) != "call":  # for print statements which donot start with "cb"
            lastindex = arr[i].rindex('p')
            printwhat =  arr[i][0:lastindex]
            p = """print(""" + printwhat + """)"""
            outfile.write(stringindent(p, indent))

        elif cbleft(arr[i], 2).lower() == "cb" and cbright(arr[i], 5).lower() != "print":  # for cbdefs
            outfile.write(stringindent(arr[i], indent))

        elif cbleft(arr[i], 2).lower() == "cb" and cbright(arr[i], 5).lower() == "print":  # printing out cbdefs
            lastindex = arr[i].rindex(')')
            command = """print(""" + arr[i][0:lastindex + 1] + """)"""
            outfile.write(stringindent(command, indent))

        elif cbleft(arr[i], 3).lower() == "end":  # for loop
            indent -= 4

        elif cbleft(arr[i], 5).lower() == "break":  # "break" in while loop
            outfile.write(stringindent("break", indent))
            indent -= 4

        elif cbleft(arr[i], 5).lower() == "while" and cbright(arr[i], 1) == ":":  # while loop
            outfile.write(stringindent("while True:", indent))
            indent += 4

        elif cbleft(arr[i], 3).lower() == "for" and cbright(arr[i], 1) == ":":  # for loop
            rang = arr[i].index("(")
            outfile.write(stringindent("for i in range" + arr[i][rang:], indent))
            indent += 4


        elif arr[i].find("=") != -1 and cbleft(arr[i], 2) != "cb" and cbleft(arr[i], 2) != "if" and cbleft(arr[i],
                                                                                                           4) != "elif" and cbleft(
                arr[i], 2) != "else" and cbright(arr[i], 3) != "int" and cbright(arr[i], 5) != "float"  :  # for "equal to" assignment operator
            equallist = list()
            for p in arr[i].split("="):
                equallist.append(p)
            outfile.write(stringindent(equallist[0] + "=" + equallist[1], indent))

        elif cbleft(arr[i], 2).lower() == "if" and cbright(arr[i], 1) == ":":  # if loop
            rang = arr[i].index("(")
            outfile.write(stringindent("if " + arr[i][rang:], indent))
            indent += 4

        elif cbleft(arr[i], 4).lower() == "elif" and cbright(arr[i], 1) == ":":  # elif loop
            rang = arr[i].index("(")
            outfile.write(stringindent("elif " + arr[i][rang:], indent))
            indent += 4

        elif cbleft(arr[i], 4).lower() == "else" and cbright(arr[i], 1) == ":":  # else loop
            outfile.write(stringindent("else :", indent))
            indent += 4


        elif arr[i].strip() == "":  # for blank lines
            pass


        elif cbright(arr[i], 3).lower() == "int" and cbright(arr[i], 5).lower() != "print" and arr[i].find("=") != -1:
            # explicit datatype conversion for int
            command = str(analyze_convert_int(arr[i],"int"))
            outfile.write(stringindent(command, indent))

        elif cbright(arr[i],5).lower() == "float" and arr[i].find("=") != -1 :
            # explicit datatype conversion for float
            command = str(analyze_convert_int(arr[i], "float"))
            outfile.write(stringindent(command, indent))


        elif cbleft(arr[i], 3) == "def" and cbright(arr[i], 1) == ":":  # functions
            command = arr[i]
            outfile.write(stringindent(command,indent))
            indent += 4

        elif cbleft(arr[i],4) == "call" and cbright(arr[i],5) != "print" : # for calling a def
            command = (arr[i][4:]).strip()
            outfile.write(stringindent(command,indent))

        elif cbleft(arr[i],4) == "call" and cbright(arr[i],5) == "print": # for calling a def
            si = (arr[i].index("l")) + 2 #starting index
            li = arr[i].rindex("p") #lastindex
            extract = arr[i][si:li]
            command = "print(" + extract + ")"
            outfile.write(stringindent(command, indent))

        if cbright(arr[i], 6) == "return": #return statement
            return_thing = arr[i][:-6]
            command = "return " + return_thing
            outfile.write(stringindent(command,indent))



def stringindent(x, lev):  # for indentation
    return chr(32) * lev + x + "\r\n"


def analyze_convert_int(x,dt):  # for converting datatype to int
    parts = list()
    for p in x.split("="):
        parts.append(p)
    if cbright(parts[1],3) == "int":
        last_index = parts[1].rindex("i")
    if cbright(parts[1],5) == "float":
        last_index = parts[1].rindex("f")
    if cbright(parts[1],3) == "str":
        last_index = parts[1].rindex("s")
    extract = parts[1][0:last_index]
    command = parts[0] + "=" + dt + "(" + extract + ")"
    return command




main()
